<!--
 * @FilePath     : /study_code/html/iframe-parent2.html
 * @Description  : iframe 不同域，调用子页面方法
 * @Date         : 2025-04-25 16:10:40
 * @Author       : Fang YingGui
 * @LastEditors  : fangyinggui 18715384437@163.com
 * @LastEditTime : 2025-04-25 16:41:02
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>documentName</title>
    <link rel="stylesheet" href="" />
  </head>
  <body>
    <h2>父页面2</h2>

    <p>下面是 iframe 嵌入的 子页面</p>
    <iframe
      id="iframe"
      src="http://localhost:3001/iframe-child2.html"
      width="500"
      height="300"
    ></iframe>

    <script src="/jquery/dist/jquery.min.js"></script>
    <script src="./sdk_parent.js"></script>
    <script>
      function parentFun() {
        console.log('父 页面方法执行')
      }

      /* $('#iframe').on('load', function () {
        console.log(1, $('#iframe'))
        console.log(2, $('#iframe')[0])
        console.log(3, $('#iframe')[0].contentWindow)
        console.log(4, $('#iframe')[0].contentWindow.childFun)
        $('#iframe')[0].contentWindow.childFun()
      }) */
      // 跨域限制，报错

      // ------
      // 父页面监听 message 事件
      var eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent'
      var eventer = window[eventMethod]
      var messageEvent = eventMethod == 'attachEvent' ? 'onmessage' : 'message'
      eventer(messageEvent, function (e) {
        console.log(e.data)
      })
    </script>
  </body>
</html>
